package com.weirong.smartvault.backservice.controller;

import com.weirong.smartvault.backservice.controller.fragment_entity.PagesIndex;
import com.weirong.smartvault.common.tools.constant.ThymleafVarName;
import com.weirong.smartvault.backservice.util.table.AuthorityTable;
import com.weirong.smartvault.backservice.util.table.Table;
import com.weirong.smartvault.backservice.util.common.ListTool;
import com.weirong.smartvault.dao.RoleRepository;
import com.weirong.smartvault.entity.Role;
import com.weirong.smartvault.backservice.entity_model.RoleModel;
import com.weirong.smartvault.backservice.user.IUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * 于2017/12/22由 KFEB4 创建 .
 */

@Controller
@RequestMapping(value = "/user")
public class User {
    @Autowired
    private RoleRepository roleRepository;

    public String userGet(ModelMap model){
        return "user/user";
    }


    @RequestMapping(value = "/role" ,method = RequestMethod.GET)
    public String roleGet(ModelMap model,@RequestParam(value = "page", defaultValue = "0") Integer page,@RequestParam(value = "size", defaultValue = "10") Integer size) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        Sort sort = new Sort(Sort.Direction.ASC, "id");
        Pageable pageable = new PageRequest(page, size, sort);
        Page<Role> rolePage = roleRepository.findAll(pageable);
        List<RoleModel> roleModels = ListTool.cast(rolePage,RoleModel.class);
        model.addAttribute(ThymleafVarName.Var.roles,roleModels);

        Map<Integer,Table> permissionTables = new HashMap<>();
        for(RoleModel roleModel:roleModels){
            permissionTables.put(roleModel.getId(), AuthorityTable.buildTable(roleModel.getAllAuthority()));
        }
        model.addAttribute(ThymleafVarName.Var.permissionTables ,permissionTables);
        model.addAttribute(ThymleafVarName.pages,new PagesIndex(page,rolePage.getTotalPages()));
        return "user/role";
    }

    @RequestMapping(value = "/role" ,method = RequestMethod.POST)
    public String rolePost(ModelMap model,@RequestParam(value = "page", defaultValue = "0") Integer page,@RequestParam(value = "size", defaultValue = "10") Integer size,Role role) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        role.setCreator(IUser.getNowUser().getId());
        role.setCreatedTime(new Date());
        roleRepository.save(role);
        return roleGet(model,page,size);
    }

    @RequestMapping(value = "/permission",method = RequestMethod.GET)
    public String permissionGet(ModelMap model,String marks) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        List<Integer> markIds = new ArrayList<>();
        if(!(marks==null)) {
            for (String mark : marks.split(",")) {
                if (mark.length()>0)
                    markIds.add(Integer.valueOf(mark));
            }
        }
        Table table =AuthorityTable.buildByRootNode(markIds);
        model.addAttribute(ThymleafVarName.Var.table,table);
        return "user/permission";
    }

}
